<template>
    <view class="content">
        <view class="mart-30">
            <view class="topContent">
                <u--form ref="uForm" :model="thisData" :rules="rules" label-width="66" labelAlign="center">
                    <u-form-item label="在用绳:" borderBottom prop="usingWireropeID" required>
                        <view class="w-100">
                            <DataSelect class="w-100" table="UsingWirerope" action="GetSearchs" :filter="{ _Status: 1 }" label="code" value="autoID" :id.sync="thisData.usingWireropeID" :name.sync="thisData.code"></DataSelect>
                        </view>
                    </u-form-item>
                    <u-form-item label="附件:" prop="attrFileList" required>
                        <view class="w-100">
                            <Upload ref="attrUploadRef" v-model="thisData.attrFileList" :limit="1" @onPreviewShow="onPreviewShow" @onPreviewHide="onPreviewHide"> </Upload>
                        </view>
                    </u-form-item>
                    <u-form-item label="备注" borderBottom prop="remark" class="relativeCol">
                        <view class="w-100">
                            <u-input v-model="thisData.remark" type="textarea" placeholder="请输入备注"> </u-input>
                        </view>
                    </u-form-item>
                </u--form>
                <view class="mart-30">
                    <u-row justify="center">
                        <u-col span="6">
                            <u-button type="primary" @click="submit">保存</u-button>
                        </u-col>
                    </u-row>
                </view>
            </view>
        </view>

        <u-toast ref="uToast"></u-toast>
    </view>
</template>

<script>
export default {
    data() {
        return {
            thisID: null,
            thisData: {
                usingWireropeID: null,
                attrFileList: [],
            },
            rules: {
                usingWireropeID: [
                    {
                        validator: (rule, value, callback) => {
                            this.$nextTick(() => {
                                uni.$emit("refreshUpload");
                            });
                            if (this.$gx.IsEmpty(value)) {
                                return callback(new Error("请选择在用绳"));
                            } else {
                                callback();
                            }
                        },
                        trigger: ["blur", "change"],
                    },
                ],
                attrFileList: [
                    {
                        validator: (rule, value, callback) => {
                            this.$nextTick(() => {
                                uni.$emit("refreshUpload");
                            });
                            if (this.$gx.IsEmpty(value) || value.length < 1) {
                                return callback(new Error("请选择附件"));
                            } else {
                                callback();
                            }
                        },
                        trigger: ["blur", "change"],
                    },
                ],
            },
        };
    },
    //生命周期-加载
    onLoad: function (option) {
        this.init(option.id);
    },
    onReady() {
        //如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
        this.$nextTick(() => {
            this.$refs.uForm.setRules(this.rules);
        });
    },
    methods: {
        init(id) {
            if (id) {
                this.thisID = id;
                App.CallApi(Api.UsingWireropeAttr.GetByID(usingWireropeAttrID.value), {
                    success: (res) => {
                        thisData.value = res;
                    },
                });
            }
        },
        submit() {
            this.$refs.uForm
                .validate()
                .then((res) => {
                    let apiFun = this.$app.api.UsingWireropeAttr.Insert;
                    if (this.thisID) {
                        apiFun = this.$app.api.UsingWireropeAttr.Update;
                    }

                    this.$app.CallApi(apiFun, {
                        data: this.thisData,
                        success: (res) => {
                            this.$refs.uToast.show({
                                type: "success",
                                title: "",
                                message: "保存成功！",
                                complete: () => {
                                    uni.navigateBack();
                                    uni.$emit("refreshData");
                                },
                            });
                        },
                    });
                })
                .catch((errors) => {});
        },
        //文件预览开启
        onPreviewShow() {
            this.$refs.attrUploadRef.setReadOnly(true);
        },
        //文件预览关闭
        onPreviewHide() {
            this.$refs.attrUploadRef.setReadOnly(false);
        },
        readMoreChange() {
            uni.$emit("refreshUpload");
        },
    },
};
</script>

<style></style>
